﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using Interfaces;

namespace Components
{
   [Export(typeof(IConfiguration))]
   public class Configuration:IConfiguration
    {
       public object getConfigurationSetting(Type expectedType, int key)
       {
           string value = System.Configuration.ConfigurationSettings.AppSettings.GetKey(key);
           if (value == null)
           {
               throw new Exception(string.Format("AppSetting:{0} is not configured.", key));
           }
           try
           {
               if (expectedType.Equals(typeof(int)))
               {
                   return int.Parse(value);
               }

               if (expectedType.Equals(typeof(string)))
               {
                   return value;  
               }

               throw new Exception("Type not supported.");
           }
           catch (Exception ex)
           {

               throw new Exception(string.Format("Config Key: {0} was expected to be of type {1} but was not.",
                   key, expectedType), ex);
           }
       }
    }
}
